//
//  WYTDestView.m
//  Diary
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "WYTDestView.h"
#import "WYTDevicesTool.h"
#import "LYLJumpInterfaceView.h"


#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#define kHomeViewSide 40 //最下面那个跳转视图

//主界面那三个按钮:打卡,日记,九宫格
#define kPageControlButtonSide 30 //边长
#define kPageControlButtonMinY kScreenHeight * 0.14 //MinY
#define kPageControlButtonGap kScreenWidth * 0.08 //间距

//铺着打卡,日记,九宫格的大ScrollView
#define kFunctionScrollViewMinY kScreenHeight * 0.208 //MinY
#define kFunctionScrollViewHeight kScreenHeight * 0.65 //高
#define kFunctionImageWidth kScreenWidth * 0.76 //贴的这三张图片的宽

//伪pageControll
#define kCircleViewSide 12 //边长
#define kCircleViewMinY kScreenHeight * 0.86 //MinY
#define kCircleViewGap kScreenWidth * 0.01932 //间距

//"关于晨间日记"
#define kShorthandDirectionButtonWidth 90 //宽
#define kShorthandDirectionButtonMinX kScreenWidth * 0.685 //MinX

//开始日记时右上角的button
#define kEndEditingButtonSide 30 //边长
#define kEndEditingButtonRightInset kScreenWidth - kScreenWidth * 0.05 - kEndEditingButtonSide //button尾巴距离右边屏边缘

//日记编辑界面
#define kDiaryBoardViewWidth kScreenWidth * 0.9 //宽
#define kDiaryBoardViewHeight kScreenHeight * 0.7 //高
//日记编辑模板ScrollView
#define kPageModelScrollViewHeight kScreenHeight * 0.07 //高
#define kPageModelScrollViewWidth kScreenHeight * 0.07 //宽
#define kPageModelScrollViewMinY kScreenHeight * 0.9 //MinY
#define kPageModelButtonSide kPageModelScrollViewHeight * 1.5 //边长

@implementation WYTDestView

#pragma mark - 准备工作
- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUps];
        [self setUpSettingsView];
    }
    return self;
}

- (void)setUps{
    [self setUpDevices];
    [self setUpScene];
}

- (void)setUpDevices{
    
}

#pragma mark - 布局
- (void)setUpScene{
    [self functionScrollViewScenes]; //主界面框架
    [self diaryScrollViewScenes]; //日记框架
}

- (void)functionScrollViewScenes{
#pragma mark 背景图片
    self.BGImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.BGImageView.image = [UIImage imageNamed:@"baseGroundFloor"];
    [self addSubview:self.BGImageView];
    
#pragma mark 基本按钮,能控制ScrollView
    self.diaryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.diaryButton.frame = CGRectMake(kScreenWidth / 2 - kPageControlButtonSide / 2, kPageControlButtonMinY, kPageControlButtonSide, kPageControlButtonSide);
    [self.diaryButton setBackgroundImage:[UIImage imageNamed:@"diaryAdd"] forState:UIControlStateNormal];
    self.diaryButton.alpha = 1;
    self.diaryButton.tag = 186001;
    [self addSubview:self.diaryButton];
    
    self.checkInButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.checkInButton.frame = CGRectMake(CGRectGetMinX(self.diaryButton.frame) - kPageControlButtonGap - kPageControlButtonSide, kPageControlButtonMinY, kPageControlButtonSide, kPageControlButtonSide);
    [self.checkInButton setImage:[UIImage imageNamed:@"checkIn_item"] forState:UIControlStateNormal];
    self.checkInButton.alpha = 0.5;
    self.checkInButton.tag = 186000;
    [self addSubview:self.checkInButton];
    
    self.shorthandButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shorthandButton.frame = CGRectMake(CGRectGetMaxX(self.diaryButton.frame) + kPageControlButtonGap, CGRectGetMinY(self.diaryButton.frame), kPageControlButtonSide, kPageControlButtonSide);
    [self.shorthandButton setImage:[UIImage imageNamed:@"shorthand_item"] forState:UIControlStateNormal];
    self.shorthandButton.alpha = 0.5;
    self.shorthandButton.tag = 186002;
    [self addSubview:self.shorthandButton];
    
#pragma mark 带有三个基本功能的ScrollView
    self.functionScrollView = [[UIScrollView alloc]init];
    self.functionScrollView.frame = CGRectMake(0, kFunctionScrollViewMinY, kScreenWidth, kFunctionScrollViewHeight);
    self.functionScrollView.backgroundColor = [UIColor whiteColor];
    self.functionScrollView.backgroundColor = [UIColor clearColor];
    self.functionScrollView.contentSize = CGSizeMake(kScreenWidth * 3, 1);
    self.functionScrollView.contentOffset = CGPointMake(kScreenWidth, 0);
    self.functionScrollView.pagingEnabled = YES;
    self.functionScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.functionScrollView];
    
#pragma mark 最底下的HomeView,用于跳转到其他界面
    self.homeKeyView = [[LYLJumpInterfaceView alloc] initWithFrame:CGRectMake(0, kScreenHeight * 0.92, kScreenWidth, kHomeViewSide)];
    [self addSubview:self.homeKeyView];
    
#pragma mark 伪PageControl的三个圆圈
    self.circleViewMid = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth - kCircleViewSide) / 2, kCircleViewMinY, kCircleViewSide, kCircleViewSide)];
    self.circleViewMid.layer.cornerRadius = kCircleViewSide / 2;
    self.circleViewMid.backgroundColor = [UIColor whiteColor];
    self.circleViewMid.alpha = 0.9;
    [self addSubview:self.circleViewMid];
    
    self.circleViewLeft = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.circleViewMid.frame) - kCircleViewSide - kCircleViewGap, CGRectGetMinY(self.circleViewMid.frame), CGRectGetWidth(self.circleViewMid.frame), CGRectGetHeight(self.circleViewMid.frame))];
    self.circleViewLeft.layer.cornerRadius = kCircleViewSide / 2;
    self.circleViewLeft.alpha = 0.3;
    self.circleViewLeft.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.circleViewLeft];
    
    self.circleViewRight = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.circleViewMid.frame) + kCircleViewGap, CGRectGetMinY(self.circleViewMid.frame), CGRectGetWidth(self.circleViewMid.frame), CGRectGetHeight(self.circleViewMid.frame))];
    self.circleViewRight.layer.cornerRadius = kCircleViewSide / 2;
    self.circleViewRight.alpha = 0.3;
    self.circleViewRight.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.circleViewRight];
    
#pragma mark 基本的大ScrollView上的ImageView大小
    self.functionImageViewA = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - kFunctionImageWidth) / 2, 0, kFunctionImageWidth, CGRectGetHeight(self.functionScrollView.frame))];
    self.functionImageViewA.image = [UIImage imageNamed:@"checkInPaper"];
    [self.functionScrollView addSubview:self.functionImageViewA];
    self.loginView = [[LoginView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.functionImageViewA.frame), CGRectGetHeight(self.functionImageViewA.frame))];
    self.loginView.backgroundColor = [UIColor clearColor];
    [self.functionImageViewA addSubview:self.loginView];
    
    self.registerView = [[RegisterView alloc]initWithFrame:CGRectMake(0, -736, CGRectGetWidth(self.functionImageViewA.frame), CGRectGetHeight(self.functionImageViewA.frame))];
    self.registerView.backgroundColor = [UIColor clearColor];
    [self.functionImageViewA addSubview:self.registerView];
    
    self.functionImageViewB = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, CGRectGetHeight(self.functionScrollView.frame))];
    self.functionImageViewB.image = [UIImage imageNamed:@"diaryPaper"];
    self.functionImageViewB.contentMode = UIViewContentModeScaleAspectFit;
    self.functionScrollView.delegate = self;
    [self.functionScrollView addSubview:self.functionImageViewB];
    
    self.functionImageViewC = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth * 2, 0, kScreenWidth, CGRectGetHeight(self.functionScrollView.frame))];
    self.functionImageViewC.image = [UIImage imageNamed:@"shorthandPaper"];
    self.functionImageViewC.contentMode = UIViewContentModeScaleAspectFit;
    [self.functionScrollView addSubview:self.functionImageViewC];
    
#pragma mark "关于晨间日记"提示按钮
    
    CGFloat shorthandDirectionButtonMinY = CGRectGetMaxY(self.functionScrollView.frame) - 10;
    NSInteger shorthandDirectionButtonFontSize = 13;
    NSInteger shorthandDirectionButtonWidth = kShorthandDirectionButtonWidth;
    CGFloat shorthandDirectionButtonMinX = kShorthandDirectionButtonMinX;
    //适配:
    if ([WYTDevicesTool iPhone6_iPhone6s]) {
        shorthandDirectionButtonFontSize = 11;
        shorthandDirectionButtonWidth = 85;
    }else if([WYTDevicesTool iPhone5_iPhone5s_iPhone5c]){
        shorthandDirectionButtonFontSize = 8;
        shorthandDirectionButtonWidth = 85;
        shorthandDirectionButtonMinX = kShorthandDirectionButtonMinX * 0.94;
    }
    
    self.shorthandDirectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shorthandDirectionButton.frame = CGRectMake(shorthandDirectionButtonMinX, shorthandDirectionButtonMinY, shorthandDirectionButtonWidth, 30);
    [self.shorthandDirectionButton setTitle:@"关于'晨间日记'" forState:UIControlStateNormal];
    self.shorthandDirectionButton.titleLabel.font = [UIFont systemFontOfSize:13];
    self.shorthandDirectionButton.alpha = 0;
    //[self addSubview:self.shorthandDirectionButton];
    
    self.shorthandDirectionButtonLineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.shorthandDirectionButton.frame), CGRectGetMaxY(self.shorthandDirectionButton.frame) - 1 - 5, CGRectGetWidth(self.shorthandDirectionButton.frame), 0.5)];
    self.shorthandDirectionButtonLineView.backgroundColor = [UIColor whiteColor];
    self.shorthandDirectionButtonLineView.alpha = 0;
    //[self addSubview:self.shorthandDirectionButtonLineView];
    
    self.diaryStartEditingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.diaryStartEditingButton.frame = CGRectMake(kScreenWidth + kScreenWidth * 0.14, CGRectGetHeight(self.functionScrollView.frame) * 0.1, kScreenWidth * 0.7, kScreenHeight * 0.55);
    self.diaryStartEditingButton.backgroundColor = [UIColor clearColor];
    [self.functionScrollView addSubview:self.diaryStartEditingButton];
    
}

- (void)diaryScrollViewScenes{
#pragma mark window bounds外的View
    //下面这三个个先初始化,给它扔到屏幕外面
    
    //主屏幕日记被点击后放大的ImageView
    self.diaryStartEditingImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-414, -736, CGRectGetWidth(self.functionImageViewB.frame), CGRectGetHeight(self.functionImageViewB.frame))];
    self.diaryStartEditingImageView.userInteractionEnabled = YES;
    self.diaryStartEditingImageView.image = [UIImage imageNamed:@"diaryPaper"];
    //self.diaryStartEditingImageView.alpha = 0.5;
    [self addSubview:self.diaryStartEditingImageView];
    //背景加毛玻璃
    self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.diaryStartEditingBlurView = [[UIVisualEffectView alloc]initWithEffect:self.blurEffect];
    self.diaryStartEditingBlurView.frame = CGRectMake(-414, -736, kScreenWidth, kScreenHeight);
    [self addSubview:self.diaryStartEditingBlurView];
    
#pragma mark 日记进入编辑的TextView
    self.diaryEditingBoardView = [[UIView alloc]initWithFrame:CGRectMake(- 736, (kScreenHeight - kDiaryBoardViewHeight) / 2, kDiaryBoardViewWidth, kDiaryBoardViewHeight)];
    [self addSubview:self.diaryEditingBoardView];
    
#pragma mark 日记编辑界面里用来选择模板的scrollView
    self.pageModelScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 800, kScreenWidth, kPageModelScrollViewHeight)];
    self.pageModelScrollView.contentSize = CGSizeMake(kPageModelButtonSide * 7, 1);
    self.pageModelScrollView.showsHorizontalScrollIndicator = NO;
    self.pageModelScrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.pageModelScrollView];
    
    //Controllers里,Button Tag值为 186010~186015
    self.pageModelABtn = [[UIButton alloc]initWithFrame:CGRectMake(kPageModelButtonSide * 0, 0,kPageModelScrollViewWidth, kPageModelScrollViewHeight)];
    [self.pageModelABtn setImage:[UIImage imageNamed:@"pageModelA"] forState:UIControlStateNormal];
    [self.pageModelScrollView addSubview:self.pageModelABtn];
    
    if ([WYTDevicesTool iPhone5_iPhone5s_iPhone5c] || [WYTDevicesTool iPhone6_iPhone6s] || [WYTDevicesTool iPhone6Plus_iPhone6sPlus]) {
        self.pageModelBBtn = [[UIButton alloc]initWithFrame:CGRectMake(kPageModelButtonSide * 1, 0,kPageModelScrollViewWidth, kPageModelScrollViewHeight)];
        [self.pageModelBBtn setImage:[UIImage imageNamed:@"pageModelC"] forState:UIControlStateNormal];
        [self.pageModelScrollView addSubview:self.pageModelBBtn];
        
        self.pageModelCBtn = [[UIButton alloc]initWithFrame:CGRectMake(kPageModelButtonSide * 2, 0,kPageModelScrollViewWidth, kPageModelScrollViewHeight)];
        [self.pageModelCBtn setImage:[UIImage imageNamed:@"pageModelD"] forState:UIControlStateNormal];
        [self.pageModelScrollView addSubview:self.pageModelCBtn];
        
        self.pageModelDBtn = [[UIButton alloc]initWithFrame:CGRectMake(kPageModelButtonSide * 3, 0,kPageModelScrollViewWidth, kPageModelScrollViewHeight)];
        [self.pageModelDBtn setImage:[UIImage imageNamed:@"pageModelE"] forState:UIControlStateNormal];
        [self.pageModelScrollView addSubview:self.pageModelDBtn];
        
        self.pageModelEBtn = [[UIButton alloc]initWithFrame:CGRectMake(kPageModelButtonSide * 4, 0,kPageModelScrollViewWidth, kPageModelScrollViewHeight)];
        [self.pageModelEBtn setImage:[UIImage imageNamed:@"pageModelF"] forState:UIControlStateNormal];
        [self.pageModelScrollView addSubview:self.pageModelEBtn];
        
        self.pageModelFBtn = [[UIButton alloc]initWithFrame:CGRectMake(kPageModelButtonSide * 5, 0,kPageModelScrollViewWidth, kPageModelScrollViewHeight)];
        [self.pageModelFBtn setImage:[UIImage imageNamed:@"pageModelB"] forState:UIControlStateNormal];
        [self.pageModelScrollView addSubview:self.pageModelFBtn];
        
        //"即将更新"
        self.pageModelWillUpdateBtn = [[UIButton alloc]initWithFrame:CGRectMake(kPageModelButtonSide * 6, 0,kPageModelScrollViewWidth, kPageModelScrollViewHeight)];
        [self.pageModelWillUpdateBtn setImage:[UIImage imageNamed:@"pageModelWillUpdate"] forState:UIControlStateNormal];
        [self.pageModelScrollView addSubview:self.pageModelWillUpdateBtn];
    }
    
    //第一次登陆后给个教程页
    BOOL firstLaunch = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
    //NSLog(@"firstLaucn = %d", firstLaunch);
    if (firstLaunch) {
        self.directionView = [[UIImageView alloc]initWithFrame:CGRectMake(-600, 0, kScreenWidth, kScreenWidth / 9 * 16)];
        self.directionView.image = [UIImage imageNamed:@"direction"];
        self.directionView.alpha = 0;
        [self addSubview:self.directionView];
        
        // 在教程页面赋值UserDefualts第一次打开的状态
        self.settingsDirectionView = [[UIImageView alloc]initWithFrame:CGRectMake(-600, 0, kScreenWidth, kScreenWidth / 9 * 16)];
        self.settingsDirectionView.image = [UIImage imageNamed:@"direction_page3"];
        self.settingsDirectionView.alpha = 0;
        [self addSubview:self.settingsDirectionView];
        UITapGestureRecognizer *tapToRemoveSettingsDirectionView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToRemoveSettingsDirectionView:)];
        self.settingsDirectionView.userInteractionEnabled = YES;
        [self.settingsDirectionView addGestureRecognizer:tapToRemoveSettingsDirectionView];
        
#warning 在教程页面赋值UserDefualts第一次打开的状态
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setBool:NO forKey:@"firstLaunch"];
        [userDefaults synchronize];
    }
}

//点击移除ScrollView第3页教程页面的方法
- (void)tapToRemoveSettingsDirectionView:(UITapGestureRecognizer *)sender{
    [UIView animateWithDuration:0.3 animations:^{
        self.settingsDirectionView.alpha = 0;
    }];
    [self performSelector:@selector(removeSettingDirectionView) withObject:self afterDelay:0.4];
}

- (void)removeSettingDirectionView{
    [self.settingsDirectionView removeFromSuperview];
}

- (void)setUpSettingsView{
    self.setttingsView = [[WYTSettingsView alloc]init];
    self.setttingsView.frame = CGRectMake(CGRectGetWidth(self.functionImageViewC.frame) * 0.0987, CGRectGetHeight(self.functionImageViewC.frame) * 0.15, CGRectGetWidth(self.functionImageViewC.frame) * 0.803, CGRectGetWidth(self.functionImageViewC.frame) * 0.803);
    [self.functionImageViewC addSubview:self.setttingsView];
}

@end
